package com.doubao.device.controller;

import com.doubao.common.result.Result;
import com.doubao.device.dto.TagDTO;
import com.doubao.device.dto.request.CreateTagRequest;
import com.doubao.device.service.SmartDeviceTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device/tag")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "智能体标签管理", description = "智能体标签相关接口")
public class SmartDeviceTagController {

    private final SmartDeviceTagService tagService;

    @PostMapping
    @Operation(summary = "创建标签", description = "创建一个新的智能体标签")
    public Result<TagDTO> createTag(@Validated @RequestBody CreateTagRequest request) {
        return tagService.createTag(request);
    }

    @DeleteMapping("/{tagId}")
    @Operation(summary = "删除标签", description = "删除指定的智能体标签")
    public Result<Void> deleteTag(
            @Parameter(description = "标签ID") @PathVariable Long tagId) {
        return tagService.deleteTag(tagId);
    }

    @GetMapping("/{tagId}")
    @Operation(summary = "获取标签详情", description = "获取指定标签的详细信息")
    public Result<TagDTO> getTagDetail(
            @Parameter(description = "标签ID") @PathVariable Long tagId) {
        return tagService.getTagDetail(tagId);
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有标签", description = "获取所有智能体标签")
    public Result<List<TagDTO>> getAllTags() {
        return tagService.getAllTags();
    }

    @GetMapping("/hot")
    @Operation(summary = "获取热门标签", description = "获取热门的智能体标签")
    public Result<List<TagDTO>> getHotTags(
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "20") Integer limit) {
        return tagService.getHotTags(limit);
    }

    @GetMapping("/type/{type}")
    @Operation(summary = "获取指定类型的标签", description = "获取指定类型的智能体标签")
    public Result<List<TagDTO>> getTagsByType(
            @Parameter(description = "标签类型") @PathVariable String type) {
        return tagService.getTagsByType(type);
    }

    @GetMapping("/device/{deviceId}")
    @Operation(summary = "获取智能体关联的标签", description = "获取指定智能体关联的标签列表")
    public Result<List<TagDTO>> getDeviceTags(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId) {
        return tagService.getDeviceTags(deviceId);
    }

    @PostMapping("/batch")
    @Operation(summary = "批量创建标签", description = "批量创建智能体标签")
    public Result<List<Long>> batchCreateTags(
            @Parameter(description = "标签名称列表") @RequestBody List<String> tagNames,
            @Parameter(description = "标签类型") @RequestParam(defaultValue = "custom") String type) {
        return tagService.batchCreateTags(tagNames, type);
    }

    @PostMapping("/device/{deviceId}")
    @Operation(summary = "添加设备标签关联", description = "为指定智能体添加标签关联")
    public Result<Void> addDeviceTags(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId,
            @Parameter(description = "标签ID列表") @RequestBody List<Long> tagIds) {
        return tagService.addDeviceTags(deviceId, tagIds);
    }

    @DeleteMapping("/device/{deviceId}")
    @Operation(summary = "移除设备标签关联", description = "移除指定智能体的标签关联")
    public Result<Void> removeDeviceTags(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId,
            @Parameter(description = "标签ID列表") @RequestBody List<Long> tagIds) {
        return tagService.removeDeviceTags(deviceId, tagIds);
    }

    @PutMapping("/device/{deviceId}")
    @Operation(summary = "更新设备标签关联", description = "更新指定智能体的标签关联")
    public Result<Void> updateDeviceTags(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId,
            @Parameter(description = "标签ID列表") @RequestBody List<Long> tagIds) {
        return tagService.updateDeviceTags(deviceId, tagIds);
    }
} 